import axios from 'axios'
import { apiConfig } from '../config/apiConfig.js'

/**
 * API 配置和实例
 */
const api = axios.create({
  timeout: apiConfig.timeout,
  headers: {
    'Content-Type': 'application/json'
  }
})

// 请求拦截器
api.interceptors.request.use(
  (config) => {
    console.log(`[${apiConfig.env}] 发送请求:`, config.url)
    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器
api.interceptors.response.use(
  (response) => {
    console.log(`[${apiConfig.env}] 收到响应:`, response.config.url)
    return response.data
  },
  (error) => {
    console.error(`[${apiConfig.env}] 请求失败:`, error.message)
    return Promise.reject(error)
  }
)

export default api
